package cn.ffcs.uec.selfstaff.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;

import cn.ffcs.uec.selfstaff.component.bean.UecSelfAssListboxBean;
import cn.ffcs.uec.selfstaff.constant.UecSelfStaffConstants;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staff.manager.UecAssManager;
import cn.ffcs.uec.staff.model.UecAss;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;
/**
 * @Title:考核信息 CRUD插件
 * @Description:
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @author Wang Yiren
 * @Version 0.0.1 date:2017年11月27日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecSelfAssListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecSelfAssListboxBean bean = new UecSelfAssListboxBean();
	/**
	 * 人员语言能力实体
	 */
	private UecAss uecAss;
	/**
	 * 人员语言能力查询实体
	 */
	// private UecAss qryUecAss;

	private UecAssManager uecAssManager = (UecAssManager) ApplicationContextUtil
			.getBean("uecAssManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecSelfAssListboxExt() {
		Executions.createComponents(UecSelfStaffConstants.UEC_ASS_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecSelfStaffConstants.ON_UEC_ASS_QUERY, this,
				UecStaffConstants.ON_UEC_ASS_QUERY_RESPONSE);

		this.addForward(UecSelfStaffConstants.ON_CLEAN_UEC_ASS, this,
				UecStaffConstants.ON_CLEAN_UEC_ASS_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecAssList() throws Exception {
		if (this.bean.getUecAssListbox().getSelectedCount() > 0) {
			uecAss = (UecAss) bean.getUecAssListbox().getSelectedItem()
					.getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecAssQueryResponse(ForwardEvent event) {
		uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
		if (null != uecStaffInfo) {
			uecAss = new UecAss();
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
					&& !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
				uecAss.setPkPsndoc(uecStaffInfo.getPkPsndoc());
			} else {
				uecAss.setStaffId(uecStaffInfo.getStaffId());
			}
			this.onQueryUecAss();
		}
	}

	public void onCleanUecAssResponse(ForwardEvent event) {
		uecStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecAssListbox());
		this.bean.getUecAssListboxPaging().setTotalSize(1);
	}


	/**
	 * 查询按钮
	 */
	public void onQueryUecAss() {
		this.bean.getUecAssListboxPaging().setActivePage(0);
		this.onQueryUecAssList();
	}


	/**
	 * 翻页
	 */
	public void onUecAssListboxPaging() {
		this.onQueryUecAssList();
	}


	/**
	 * 查询
	 */
	public void onQueryUecAssList() {
		if (null != uecAss) {
			PageInfo pageInfo = uecAssManager.queryPageInfoByUecAss(uecAss,
					this.bean.getUecAssListboxPaging().getActivePage() + 1,
					this.bean.getUecAssListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecAssListbox().setModel(list);
			this.bean.getUecAssListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
		}
	}

}
